package com.dahuyou.tryy.too.simulate.interpreter.parse.clazz.clazzfile.attributeinfo.impl;

import com.dahuyou.tryy.too.simulate.interpreter.parse.clazz.clazzfile.ClassReader;
import com.dahuyou.tryy.too.simulate.interpreter.parse.clazz.clazzfile.attributeinfo.AttributeInfo;
import com.dahuyou.tryy.too.simulate.interpreter.parse.clazz.clazzfile.constantpool.ConstantPool;

import java.util.HashMap;
import java.util.Map;

public class EnclosingMethodAttribute implements AttributeInfo {

    private ConstantPool constantPool;
    private int classIdx;
    private int methodIdx;


    public EnclosingMethodAttribute(ConstantPool constantPool) {
        this.constantPool = constantPool;
    }

    @Override
    public void readInfo(ClassReader reader) {
//        this.classIdx = reader.readUint16();
        this.classIdx = reader.readU2();
//        this.methodIdx = reader.readUint16();
        this.methodIdx = reader.readU2();
    }

    public String className() {
        return this.constantPool.getClassName(this.classIdx);
    }

    public Map<String, String> methodNameAndDescriptor() {
        if (this.methodIdx <= 0) return new HashMap<>();
        return this.constantPool.getNameAndType(this.methodIdx);
    }

}
